package cn.iocoder.yudao.module.ai.controller.admin.trainingmodel;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ai.dal.dataobject.algorithmlibrary.AlgorithmLibraryDO;
import cn.iocoder.yudao.module.ai.utils.AssertUtil;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;



import cn.iocoder.yudao.module.ai.controller.admin.trainingmodel.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.trainingmodel.TrainingModelDO;
import cn.iocoder.yudao.module.ai.service.trainingmodel.TrainingModelService;

@Tag(name = "管理后台 - 训练模型清单")
@RestController
@RequestMapping("/ai/training-model")
@Validated
public class TrainingModelController {

    @Resource
    private TrainingModelService trainingModelService;

    @Resource
    private DeptApi deptApi;
    /**
     * 创建训练模型：
     *    1.创建训练模型，供应商为部门id
     *    2.除平台管理员外，数据权限仅本部门可见。
     *    3.数据权限配置类：ModelDataPermissionConfiguration
     *    4.创建后模型状态为：草稿
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建训练模型")
    @PreAuthorize("@ss.hasPermission('ai:training-model:create')")
    public CommonResult<Integer> createTrainingModel(@Valid @RequestBody TrainingModelSaveReqVO createReqVO) {
        createReqVO.setSuplierId(deptApi.queryFirstDept(SecurityFrameworkUtils.getLoginUserDeptId()));
        //todo 状态硬编码,替换成字典值
        createReqVO.setStatus("0");
        return success(trainingModelService.createTrainingModel(createReqVO));
    }

    /**
     * 查询草稿箱
     * 功能：
     *      分页查询个人草稿文件
     * @param pageReqVO
     * @return
     */
    @GetMapping("/ownDraftPage")
    @Operation(summary = "查询草稿箱")
    @PreAuthorize("@ss.hasPermission('ai:training-model:own')")
    public CommonResult<PageResult<TrainingModelRespVO>> getOwnModelDraftPage(@Valid TrainingModelPageReqVO pageReqVO) {
        PageResult<TrainingModelDO> pageResult = trainingModelService.queryOwnModelDraft(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TrainingModelRespVO.class));
    }

    /**
     * 发布模型草稿：
     * 校验：
     *      1.仅允许发布自己创建的模型。
     *      2.仅允许发布草稿状态的模型。
     * 功能：
     *      1.将模型状态由 草稿---->变更为 待审核
     * @param publishVo
     * @return
     */
    @PutMapping("/publishModelDraft")
    @Operation(summary = "发布模型草稿")
    @PreAuthorize("@ss.hasPermission('ai:training-model:publish')")
    public CommonResult<Boolean> publishModelDraft(@Valid @RequestBody TrainingModelSaveReqVO publishVo) {
        TrainingModelDO trainingModel = trainingModelService.getTrainingModel(publishVo.getId());
        AssertUtil.equal(SecurityFrameworkUtils.getLoginUserId()+"",trainingModel.getCreator(),"仅允许本人发布模型。");
        AssertUtil.equal(trainingModel.getStatus(),"0","仅允许发布状态 《草稿》 的模型。");
        //todo 状态硬编码,替换成字典值
        publishVo.setStatus("1");
        trainingModelService.updateTrainingModel(publishVo);
        return success(true);
    }

    /**
     * 更新训练模型：
     *     1.校验权限仅创建者本人才能修改
     * @param updateReqVO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新训练模型清单")
    @PreAuthorize("@ss.hasPermission('ai:training-model:update')")
    public CommonResult<Boolean> updateTrainingModel(@Valid @RequestBody TrainingModelSaveReqVO updateReqVO) {
        TrainingModelDO trainingModel = trainingModelService.getTrainingModel(updateReqVO.getId());
        AssertUtil.equal(SecurityFrameworkUtils.getLoginUserId()+"",trainingModel.getCreator(),"仅允许本人修改模型。");
        trainingModelService.updateTrainingModel(updateReqVO);
        return success(true);
    }

    /**
     * 删除训练模型：
     *     1.校验权限仅创建者本人才能删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除训练模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:training-model:delete')")
    public CommonResult<Boolean> deleteTrainingModel(@RequestParam("id") Integer id) {
        TrainingModelDO trainingModel = trainingModelService.getTrainingModel(id);
        AssertUtil.equal(SecurityFrameworkUtils.getLoginUserId()+"",trainingModel.getCreator(),"仅允许本人修改模型。");
        trainingModelService.deleteTrainingModel(id);
        return success(true);
    }

    /**
     * 查询单条训练模型记录：
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "查询单条训练模型记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:training-model:query')")
    public CommonResult<TrainingModelRespVO> getTrainingModel(@RequestParam("id") Integer id) {
        TrainingModelDO trainingModel = trainingModelService.getTrainingModel(id);
        return success(BeanUtils.toBean(trainingModel, TrainingModelRespVO.class));
    }

    /**
     * 获得训练模型清单分页：
     * @param pageReqVO
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "获得训练模型清单分页")
    @PreAuthorize("@ss.hasPermission('ai:training-model:query')")
    public CommonResult<PageResult<TrainingModelRespVO>> getTrainingModelPage(@Valid TrainingModelPageReqVO pageReqVO) {
        PageResult<TrainingModelDO> pageResult = trainingModelService.getTrainingModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TrainingModelRespVO.class));
    }



}